package com.ndp.fb.walle.business.impl;

import com.alibaba.dubbo.config.annotation.Reference;
import com.ndp.fb.exception.BusinessException;
import com.ndp.fb.rdb.api.AdwordRdbService;
import com.ndp.fb.rdb.api.ConnectionObjectAdwordsRdbService;
import com.ndp.fb.rdb.model.Adword;
import com.ndp.fb.rdb.model.Dictionary;
import com.ndp.fb.rdb.model.Pager;
import com.ndp.fb.rdb.model.User;
import com.ndp.fb.rdb.model.adword.ConnectionObjectAdwords;
import com.ndp.fb.walle.business.AdwordService;
import com.ndp.fb.walle.model.vo.adword.param.AdwordParam;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;

/**
 * Title: AdwordServiceImpl.java
 * Description: 描述
 *
 * @author Administrator
 *         创建时间 2014年2月28日 下午3:15:58
 */
@Component
public class AdwordServiceImpl implements AdwordService {
    @Reference
    private AdwordRdbService adwordRdbService;

    @Reference
    private ConnectionObjectAdwordsRdbService connectionObjectAdwordsRdbServices;

    @Override
    public Long createAdword(User user, AdwordParam adwordParam) throws BusinessException {
        String userId = user.getTicket();

        Long productId = adwordParam.getProductId();
        Long orgId = user.getOrgId();
        String description = adwordParam.getDescription();
        Long languageId = adwordParam.getLanguageId();
        String word = adwordParam.getWord();

        Adword adword = adwordRdbService.findByProductIdAndOrgIdAndLanaguageAndWord(productId,orgId,languageId,word);
        if (adword == null) {
            adword = new Adword();
            adword.setOrgId(orgId);
            adword.setUserId(userId);
            adword.setCreateTime(new Date());
            adword.setWord(word);
            adword.setDescription(description);
            adword.setDictId(languageId);
            adword.setProductId(productId);

            Long adwordId = adwordRdbService.saveAdword(adword);
            adword.setId(adwordId);
        }

        return adword.getId();
    }

    @Override
    public Boolean deleteAdword(String userId, Long id) {
        adwordRdbService.deleteByIdAndUserId(userId, id);
        connectionObjectAdwordsRdbServices.deleteByAdwordId(id);
        return true;
    }

    @Override
    public Adword findById(long id) {
        return adwordRdbService.findAdwordById(id);
    }

    @Override
    public List<Dictionary> searchLanguage(Long fbConnectionObjectId, Long orgId, String language) {
        if ("zh-cn".equals(language) || "zh_CN".equals(language) || "zh".equals(language) || "CN".equals(language)) {
            if (null == fbConnectionObjectId || -1 == fbConnectionObjectId){
                //查询该公司下设置过的语言
                return adwordRdbService.searchLanguageByOrg(orgId);
            } else {
                //查询指定应用下，设置过的语言
                return adwordRdbService.searchLanguageByApp(fbConnectionObjectId, orgId);
            }
        } else  {
            if (null == fbConnectionObjectId || -1 == fbConnectionObjectId){
                //查询该公司下设置过的语言
                return adwordRdbService.searchLanguageByOrgEN(orgId);
            } else {
                //查询指定应用下，设置过的语言
                return adwordRdbService.searchLanguageByAppEN(fbConnectionObjectId, orgId);
            }
        }

    }

    @Override
    public List<Adword> getAdwordsAllList(Long productId, Long languageId, Long orgId) {
        if (null == productId || -1 == productId){
            //查询该公司下的广告语
            return adwordRdbService.findByOrgIdAndDictId(orgId, languageId);
        } else {
            //查询指定应用下的广告语
            return adwordRdbService.findByProductIdAndOrgIdAndLanaguage(productId, orgId, languageId);
        }
    }

    @Override
    public Pager<Adword> getAdwordsList(Long productId, Long languageId, Long orgId, int firstResult, int sizeNo) {
        if (null == productId || -1 == productId){
            return (Pager<Adword>)adwordRdbService.findPagerByOrgIdAndDictId(orgId, languageId, firstResult, sizeNo);
        } else {
            return (Pager<Adword>)adwordRdbService.findPagerByProductIdAndOrgIdAndDictId(productId,
                    orgId, languageId, firstResult, sizeNo);
        }
    }

    @Override
    public List<Adword> findByProductId(Long productId) {
        return adwordRdbService.findAdwordByProductId(productId);
    }

    @Override
    public List<Dictionary> searchLanguage(Long fbConnectionObjectId, String dicType, Long orgId, String language) {
        if ("zh-cn".equals(language) || "zh_CN".equals(language) || "zh".equals(language) || "CN".equals(language)) {
            if (null == fbConnectionObjectId || -1 == fbConnectionObjectId){
                //查询该公司下设置过的语言
                return adwordRdbService.searchLanguageByOrg(orgId,dicType);
            } else {
                //查询指定应用下，设置过的语言
                return adwordRdbService.searchLanguageByApp(fbConnectionObjectId, orgId,dicType);
            }
        } else  {
            if (null == fbConnectionObjectId || -1 == fbConnectionObjectId){
                //查询该公司下设置过的语言
                return adwordRdbService.searchLanguageByOrgEN(orgId,dicType);
            } else {
                //查询指定应用下，设置过的语言
                return adwordRdbService.searchLanguageByAppEN(fbConnectionObjectId, orgId,dicType);
            }
        }
    }

	@Override
	public Adword findRandomForRobot(Long orgId, Long productId, List<String> language) {
		return adwordRdbService.findRandomForRobot(orgId, productId, language);
	}
}